#!/bin/bash

# This builds a list of dates going back 30 days
dates=''
for i in $(seq 0 30) ; do
	tmpdate=$(date -d "$i days ago" +%Y%m%d)
	dates="$dates $tmpdate"
done

for vserver in $(ls /logs/vservers/) ; do
	logs=$(ls /logs/vservers/$vserver/$vserver.log.*.gz 2>/dev/null)
	dellist=''
	for log in $logs; do

		# This tries to match the log datestamp to the list of
		# dates that are less than 60 days.  If we match the list
		# of dates, we mark the log for retention

		keep=0
		for date in $dates; do
			if [ "$log" != "${log/$date/}" ]; then
				keep=1
				break
			fi
		done
		if [ $keep != 1 ]; then
			dellist="$dellist $log"
		fi
	done
	# this should cope with cases where the list
	# is too long for a single rm invocation.

	[ -z "$dellist" ] || echo $dellist | xargs --no-run-if-empty rm -f
done
